import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
import { ConfigProvider } from 'antd';
import { useAuthStore } from './stores/authStore';
import Login from './pages/Login';
import Dashboard from './pages/Dashboard';
import TaskManagement from './pages/TaskManagement';
import WorkflowManagement from './pages/WorkflowManagement';
import Home from '@/pages/Home';

// 受保护的路由组件
function ProtectedRoute({ children }: { children: React.ReactNode }) {
  const { isAuthenticated } = useAuthStore();
  return isAuthenticated ? <>{children}</> : <Navigate to="/login" replace />;
}

// 公开路由组件（已登录用户重定向到仪表板）
function PublicRoute({ children }: { children: React.ReactNode }) {
  const { isAuthenticated } = useAuthStore();
  return !isAuthenticated ? <>{children}</> : <Navigate to="/dashboard" replace />;
}

export default function App() {
  return (
    <ConfigProvider
      theme={{
        cssVar: true,
        hashed: false,
      }}
    >
      <Router>
        <Routes>
        {/* 公开路由 */}
        <Route 
          path="/login" 
          element={
            <PublicRoute>
              <Login />
            </PublicRoute>
          } 
        />
        
        {/* 受保护的路由 */}
        <Route 
           path="/dashboard" 
           element={
             <ProtectedRoute>
               <Dashboard />
             </ProtectedRoute>
           } 
         />
         
         <Route 
           path="/tasks" 
           element={
             <ProtectedRoute>
               <TaskManagement />
             </ProtectedRoute>
           } 
         />
         
         <Route 
           path="/workflows" 
           element={
             <ProtectedRoute>
               <WorkflowManagement />
             </ProtectedRoute>
           } 
         />
         
         <Route 
           path="/" 
           element={
             <ProtectedRoute>
               <Home />
             </ProtectedRoute>
           } 
         />
        
        {/* 默认重定向 */}
        <Route path="*" element={<Navigate to="/dashboard" replace />} />
      </Routes>
    </Router>
    </ConfigProvider>
  );
}
